package com.cjh.db2doc.template;

import com.cjh.db2doc.table.TableInfo;
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.filechooser.FileSystemView;

/**
 * com.cjh.db2doc.template 2022/7/26 18:01
 *
 * @author chenjh
 */
public class FreemarkerEngine {

    public static void generateWord(List<TableInfo> tableInfoList) throws Exception {
        //1.创建配置类
        Configuration configuration = new Configuration(Configuration.getVersion());
        //2.设置模板所在的目录
        configuration.setTemplateLoader(
                new ClassTemplateLoader(FreemarkerEngine.class, "/freemarker/"));
        //3.设置字符集
        configuration.setDefaultEncoding("utf-8");
        //4.加载模板
        Template template = configuration.getTemplate("word.ftl");
        //5.创建数据模型
        Map<String, Object> map = new HashMap();
        map.put("tableList", tableInfoList);
        //6.创建Writer对象
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File desktop = fsv.getHomeDirectory();
        Writer out = new FileWriter(desktop + "\\生成.docx");
        //7.输出
        template.process(map, out);
        //8.关闭Writer对象
        out.close();
    }
}
